/*
 * Copyright 2007 Aleksandar Pantaleev
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.lang.reflect.*;

/**
 * Provides basic checks for equivalence of methods, fields, and interfaces of two classes
 */

public class ReflectionChecker {
  private Class version1;
  private Class version2;
  
  public ReflectionChecker(Class c1, Class c2) {
    version1 = c1;
    version2 = c2;
  }
  
  public void checkFields() {
    Field[] arr1 = version1.getDeclaredFields();
    Field[] arr2 = version2.getDeclaredFields();
    for (int i = 0; i < arr1.length; i++) {
      boolean flag = false;
      System.out.println(arr1[i].toGenericString());
      for (int j = 0; j < arr2.length; j++) {
        if (arr2[j] == null)
          continue;
        System.out.println(arr2[j].toGenericString());
        if (arr1[i].toGenericString().equals(arr2[j].toGenericString())) {
          flag = true;
          arr2[j] = null;
          break;
        }
      }
      if (!flag)
        System.out.println("diff1: " + arr1[i].toGenericString());
    }
    
    for (int j = 0; j < arr2.length; j++)
      if (arr2[j] != null)
        System.out.println("diff2: " + arr2[j].toGenericString());
  }
  
  public void checkInterfaces() {
    Class[] arr1 = version1.getInterfaces();
    Class[] arr2 = version2.getInterfaces();
    for (int i = 0; i < arr1.length; i++) {
      boolean flag = false;
      System.out.println(arr1[i].toString());
      for (int j = 0; j < arr2.length; j++) {
        if (arr2[j] == null)
          continue;
        System.out.println(arr2[j].toString());
        if (arr1[i].toString().equals(arr2[j].toString())) {
          flag = true;
          arr2[j] = null;
          break;
        }
      }
      if (!flag)
        System.out.println("diff1: " + arr1[i].toString());
    }
    
    for (int j = 0; j < arr2.length; j++)
      if (arr2[j] != null)
        System.out.println("diff2: " + arr2[j].toString());
  }
  
  public void checkMethods() {
    Method[] arr1 = version1.getDeclaredMethods();
    Method[] arr2 = version2.getDeclaredMethods();
    for (int i = 0; i < arr1.length; i++) {
      boolean flag = false;
      System.out.println(arr1[i].toGenericString());
      for (int j = 0; j < arr2.length; j++) {
        if (arr2[j] == null)
          continue;
        System.out.println(arr2[j].toGenericString());
        if (arr1[i].toGenericString().equals(arr2[j].toGenericString())) {
          flag = true;
          arr2[j] = null;
          break;
        }
      }
      if (!flag)
        System.out.println("diff1: " + arr1[i].toGenericString());
    }
    
    for (int j = 0; j < arr2.length; j++)
      if (arr2[j] != null)
        System.out.println("diff2: " + arr2[j].toGenericString());
  }
  
}
